export default {
  // 加法
  add(num1, num2) {
    const num1Digits = (num1.toString().split('.')[1] || '').length
    const num2Digits = (num2.toString().split('.')[1] || '').length
    const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits))
    return (num1 * baseNum + num2 * baseNum) / baseNum
  },

  // 减法 
  subtract(num1, num2) {
    const num1Digits = (num1.toString().split('.')[1] || '').length
    const num2Digits = (num2.toString().split('.')[1] || '').length
    const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits))
    return (num1 * baseNum - num2 * baseNum) / baseNum
  },

  // 乘法
  multiply(num1, num2) {
    const num1Digits = (num1.toString().split('.')[1] || '').length
    const num2Digits = (num2.toString().split('.')[1] || '').length
    const baseNum = Math.pow(10, num1Digits + num2Digits)
    return (num1 * baseNum * (num2 * baseNum)) / (baseNum * baseNum)
  },

  // 除法
  divide(num1, num2) {
    const num1Digits = (num1.toString().split('.')[1] || '').length
    const num2Digits = (num2.toString().split('.')[1] || '').length
    const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits))
    return (num1 * baseNum) / (num2 * baseNum)
  },

  // 格式化金额
  formatPrice(price) {
    if (!price && price !== 0) return '0.00'
    const [integer, decimal] = Number(price).toString().split('.')
    if (!decimal) return integer + '.00'
    if (decimal.length === 1) return integer + '.' + decimal + '0'
    if (decimal.length > 2) {
      const rounded = Math.round(Number('0.' + decimal) * 100) / 100
      return integer + '.' + (rounded.toString().split('.')[1] || '00')
    }
    return integer + '.' + decimal
  }
} 